//主控制器
interface Imain extends ng.IScope{
app:any;

}

class mainController{
    
    storeUserInfo: string;
    constructor(private $scope:Imain,private wilddogService:wilddogService,private $rootScope:ImainRootScope,private routeService:routeService,private toaster:any,private $state:ng.ui.IStateService,private menuService:IMenuService){
         $rootScope.$on('$stateChangeStart',(event, toState, toParams, fromState, fromParams)=>{
            if(window.localStorage.getItem('loginData')){
                this.storeUserInfo = JSON.parse(window.localStorage.getItem('loginData')).id;
            }
             if(toState.name !=="login.in"){
                 $("#login").removeClass("bg2login")
                 $("#login").removeClass("bglogin")
             }
             if (toState.name == 'login.in') {
                 $("#login").addClass("bglogin")
                 return
            };
             if(!window.localStorage.getItem("loginData")||window.localStorage.getItem("logToken")=="undefined"){ 
                event.preventDefault()
                this.$state.go("login.in");
                return;
            }
         });
        this.init();

        // config
        $scope.app = {
          name: '智运通',
          version: '1.3.3',
          color: {
              primary: '#7266ba',
              info: '#23b7e5',
              success: '#27c24c',
              warning: '#fad733',
              danger: '#f05050',
              light: '#e8eff0',
              dark: '#3a3f51',
              black: '#1c2b36'
          },
          settings: {
              themeID: 1,
              navbarHeaderColor: 'bg-black',
              navbarCollapseColor: 'bg-white-only',
              asideColor: 'bg-black',
              headerFixed: true,
              asideFixed: false,
              asideFolded: false,
              asideDock: false,
              container: false
          }
          
      };

    }

    init():void{
        console.log('main controller loaded 15:15')
        bootbox.setLocale("zh_CN");//bootbox弹出框设置中文
        
        window.onstorage = (e) =>{
            if(e.storageArea.length > 0 && e.key==="loginData"){
                var current = JSON.parse(e.storageArea['loginData']).id;
                if(current !== this.storeUserInfo){
                    console.log("同一浏览器只能登录一个用户谢谢");
                    this.$state.go("login.in");
                }
            }
        }
        if(!this.$rootScope.headPath){
            this.$rootScope.headPath=window.localStorage.getItem('headPath');
        }

        jQuery.validator.addMethod("mile-pattern",function(value,element,param){

            // console.log(value,element,param);

            const reg = new RegExp(param.replace(/\//g,''));
            const rr = value.match(reg)
            
            
    
            // console.log(element)

            if(value && !rr){
                $(element).addClass('error').removeClass('ng-valid')
                return false;

            }else{
                $(element).removeClass('error').addClass('ng-valid')
                return true
            }

            
        },"请输入正确的里程数")


       

    }


}
angular.module("tms").controller("mainController",["$scope","wilddogService","$rootScope","routeService",'toaster','$state','menuService',mainController])